#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    // 新增行
    ui->tableWidget->insertRow(0);
    ui->tableWidget->insertRow(1);
    ui->tableWidget->insertRow(2);

    // 新增列
    ui->tableWidget->insertColumn(0);
    ui->tableWidget->insertColumn(1);
    ui->tableWidget->insertColumn(2);

    // 设置列名
    ui->tableWidget->setHorizontalHeaderItem(0, new QTableWidgetItem("班级"));
    ui->tableWidget->setHorizontalHeaderItem(1, new QTableWidgetItem("姓名"));
    ui->tableWidget->setHorizontalHeaderItem(2, new QTableWidgetItem("学号"));

    // 设置元素
    ui->tableWidget->setItem(0, 0, new QTableWidgetItem("计科2201"));
    ui->tableWidget->setItem(0, 1, new QTableWidgetItem("syh"));
    ui->tableWidget->setItem(0, 2, new QTableWidgetItem("22"));

    ui->tableWidget->setItem(1, 0, new QTableWidgetItem("计科2202"));
    ui->tableWidget->setItem(1, 1, new QTableWidgetItem("zzl"));
    ui->tableWidget->setItem(1, 2, new QTableWidgetItem("36"));

    ui->tableWidget->setItem(2, 0, new QTableWidgetItem("计科2202"));
    ui->tableWidget->setItem(2, 1, new QTableWidgetItem("yun"));
    ui->tableWidget->setItem(2, 2, new QTableWidgetItem("01"));

}

Widget::~Widget()
{
    delete ui;
}


void Widget::on_pushButton_insertRow_clicked()
{
    // 获取行数
    int rowCount = ui->tableWidget->rowCount();

    // 在最后一行之后新增行
    ui->tableWidget->insertRow(rowCount);
}

void Widget::on_pushButton_removeRow_clicked()
{
    // 获取到选中的行
    int curRow = ui->tableWidget->currentRow();

    // 删除选中行
    ui->tableWidget->removeRow(curRow);
}

void Widget::on_pushButton_insertColumn_clicked()
{
    // 获取列数
    int colCount = ui->tableWidget->columnCount();

    // 在最后一行之后新增行
    ui->tableWidget->insertColumn(colCount);

    // 设置列名
    const QString& text = ui->lineEdit->text();
    ui->tableWidget->setHorizontalHeaderItem(colCount, new QTableWidgetItem(text));
}

void Widget::on_pushButton_removeColumn_clicked()
{
    // 获取选中的列
    int curCol = ui->tableWidget->currentColumn();

    // 删除选中列
    ui->tableWidget->removeColumn(curCol);
}
